package cn.rkylin.oms.warehouseDocking.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.DateUtil;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.warehouse.returnStockin.vo.ReturnStockinVO;
import cn.rkylin.oms.warehouse.shiporder.vo.ShiporderVO;
import cn.rkylin.oms.warehouseDocking.adapter.WarehouseDockingAdapter;
import cn.rkylin.oms.warehouseDocking.adapter.WarehouseDockingFactory;

@Controller
@RequestMapping("/warehouseDockingController")
public class WarehouseDockingController {
	private static final Log logger = LogFactory.getLog(WarehouseDockingController.class);
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	LogUtil logUtil;

	/**
	 * 商品同步接口 http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * synchronizeGoods.action
	 * 
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/synchronizeGoods", method = RequestMethod.GET)
	public Map<String, Object> synchronizeGoods(String shopId) {
		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 注意: 此处是prj_id
		// shopId = "c06b842a53ee452997cecd8d0230acac";

		try {
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.synchronizeGoods(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());

			logUtil.error(new DBLog("", "job_exception", "synchronizeGoods",
					DateUtil.getCurrentDateStr() + "  自动任务  商品同步接口异常: " + e.getMessage()), e);

		}

		return resultMap;
	}

	/**
	 * 其他出库单创建接口
	 * http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * otherStockoutCreate.action
	 * 
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/otherStockoutCreate", method = RequestMethod.GET)
	public Map<String, Object> otherStockoutCreate(String shopId) {

		// 注意: 此处是prj_id
		// shopId = "c06b842a53ee452997cecd8d0230acac";

		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.otherStockoutCreate(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {

			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());

			logUtil.error(new DBLog("", "job_exception", "otherStockoutCreate",
					DateUtil.getCurrentDateStr() + "  自动任务  其他出库单创建接口异常: " + e.getMessage()), e);

		}

		return resultMap;
	}

	/**
	 * 取消退货入库单 http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * cancelReturnStockin.action
	 * 
	 * @param returnStockInVO
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@ResponseBody
	@RequestMapping(value = "/cancelReturnStockin", method = RequestMethod.GET)
	public Map<String, String> cancelReturnStockin(String returnStockInVO) {

		// 返回值
		Map<String, String> resultMap = new HashMap<String, String>();

		try {
			String returnStockInVOStr = URLDecoder.decode(returnStockInVO);
			ReturnStockinVO returnStockinData = JSON.parseObject(returnStockInVOStr, ReturnStockinVO.class);
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(returnStockinData.getStockinId());
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			int r = warehouseDockingAdapter.cancelReturnStockin(returnStockinData);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}

		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());

			logUtil.error(new DBLog("", "job_exception", "cancelReturnStockin",
					DateUtil.getCurrentDateStr() + "  自动任务  取消退货入库单接口异常: " + e.getMessage()), e);
		}

		return resultMap;
	}

	/**
	 * 手动取消出库单 http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * manualCancelShiporder.action
	 * 
	 * @param shiporderVO
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@ResponseBody
	@RequestMapping(value = "/manualCancelShiporder", method = RequestMethod.GET)
	public Map<String, String> manualCancelShiporder(String shiporderVO) {

		// 返回值
		Map<String, String> resultMap = new HashMap<String, String>();

		try {
			String shiporderVoStr = URLDecoder.decode(shiporderVO);
			ShiporderVO shiporderData = JSON.parseObject(shiporderVoStr, ShiporderVO.class);
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shiporderData.getShopId());
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			int r = warehouseDockingAdapter.manualCancelShiporder(shiporderData);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());
			logUtil.error(new DBLog("", "job_exception", "manualCancelShiporder",
					DateUtil.getCurrentDateStr() + "  自动任务  手动取消发货单异常: " + e.getMessage()), e);
		}

		return resultMap;
	}

	/**
	 * 自动取消出库单 http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * autoCancelShiporder.action
	 * 
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/autoCancelShiporder", method = RequestMethod.GET)
	public Map<String, Object> autoCancelShiporder(String shopId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.autoCancelShiporder(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());
			logUtil.error(new DBLog("", "job_exception", "autoCancelShiporder",
					DateUtil.getCurrentDateStr() + "  自动任务  自动取消发货单异常: " + e.getMessage()), e);
		}

		return resultMap;
	}

	/**
	 * 发货单创建接口 http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * shipOrderCreate.action
	 *
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/shipOrderCreate", method = RequestMethod.GET)
	public Map<String, Object> shipOrderCreate(String shopId) {

		// 注意: 此处是prj_id
		// shopId = "c06b842a53ee452997cecd8d0230acac";

		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.shipOrderCreate(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());

			logUtil.error(new DBLog("", "job_exception", "shipOrderCreate",
					DateUtil.getCurrentDateStr() + "  自动任务  发货单创建接口异常: " + e.getMessage()), e);
		}

		return resultMap;
	}

	/**
	 * 获取仓库库存(多商品)接口
	 * http://localhost:8081/OMSJobExecutor/warehouseDockingController/
	 * getInventory.action
	 *
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getInventory", method = RequestMethod.GET)
	public Map getInventory(String shopId) {

		// 注意: 此处是prj_id
		// shopId = "c06b842a53ee452997cecd8d0230acac";

		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.getInventory(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());
			logUtil.error(new DBLog("", "job_exception", "getInventory",
					DateUtil.getCurrentDateStr() + "  自动任务  获取仓库库存(多商品)异常: " + e.getMessage()), e);
		}

		return resultMap;
	}

	/**
	 * 退货入库单创建接口
	 * http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * returnOrderCreate.action
	 *
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/returnOrderCreate", method = RequestMethod.GET)
	public Map<String, Object> returnOrderCreate(String shopId) {
		// 注意: 此处是prj_id
		// shopId = "c06b842a53ee452997cecd8d0230acac";

		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {

			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.returnOrderCreate(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {

			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());

			logUtil.error(new DBLog("", "job_exception", "returnOrderCreate",
					DateUtil.getCurrentDateStr() + "  自动任务  退货入库单创建接口异常: " + e.getMessage()), e);
		}

		return resultMap;
	}

	/**
	 * 入库单创建接口 http://localhost:8080/OMSJobExecutor/warehouseDockingController/
	 * entryOrderCreate.action
	 *
	 * @param shopId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/entryOrderCreate", method = RequestMethod.GET)
	public Map<String, Object> entryOrderCreate(String shopId) {
		// 注意: 此处是prj_id
		// shopId = "c06b842a53ee452997cecd8d0230acac";

		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			WarehouseDockingAdapter warehouseDockingAdapter = WarehouseDockingFactory
					.getWarehouseDockingAdapter(shopId);
			if (warehouseDockingAdapter == null) {
				resultMap.put("success", "false");
				resultMap.put("msg", "WarehouseDockingAdapter is null");
				return resultMap;
			}

			Map<String, Object> reqMap = new HashMap<String, Object>();
			int r = warehouseDockingAdapter.entryOrderCreate(shopId, reqMap);
			if (r == 0) {
				resultMap.put("success", "false");
			} else {
				resultMap.put("success", "true");
			}
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());

			logUtil.error(new DBLog("", "job_exception", "entryOrderCreate",
					DateUtil.getCurrentDateStr() + "  自动任务  入库单创建接口异常: " + e.getMessage()), e);
		}
		return resultMap;
	}
}
